import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

bmi_life_data = pd.read_csv('bmi_and_life_expectancy.csv')

x = np.array(bmi_life_data[["BMI"]])
y = np.array(bmi_life_data["Life expectancy"])

def draw_data(x, y):
    for i in range(len(x)):
        plt.scatter(x[i], y[i], color='blue', edgecolor='k')
    plt.xlabel('BMI')
    plt.ylabel('Life expectancy')

def display(m, b, color='g'):
    r = np.arange(min(x), max(x), 0.1)
    plt.plot(r, m*r+b, color)


epochs = 1000
learning_rate = 0.001


def linear_regression(x, y):
    w = 1
    b = 0
    for epoch in range(epochs):
        for i in range(len(x)):
            y_hat = w * x[i] + b
            delt = learning_rate * (y[i] - y_hat)

            w += w * delt
            b += delt
    return w, b

def predict(w, b, bmi):
    return w[0] * bmi + b[0]


w, b = linear_regression(x, y)


draw_data(x, y)
display(w[0], b[0])
plt.show()